package com.castle.fortress.admin.goods.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsDto;
import com.castle.fortress.admin.goods.entity.GoodsEntity;
import com.castle.fortress.admin.goods.service.GoodsService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品表 api 控制器
 *
 * @author majunjie
 * @since 2021-11-22
 */
@Api(tags="商品表api管理控制器")
@RestController
@RequestMapping("/api/goods/goods")
public class ApiGoodsController {
    @Autowired
    private GoodsService goodsService;


    /**
     * 商品表的分页展示
     * @param goodsDto 商品表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("商品表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsDto>> pageGoods(GoodsDto goodsDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsDto> pages = goodsService.pageGoods(page, goodsDto);
        return RespBody.data(pages);
    }

    /**
     * 商品表保存
     * @param goodsDto 商品表实体类
     * @return
     */
    @ApiOperation("商品表保存")
    @PostMapping("/save")
    public RespBody<String> saveGoods(@RequestBody GoodsDto goodsDto){
        if(goodsDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsEntity goodsEntity = ConvertUtil.transformObj(goodsDto,GoodsEntity.class);
        if(goodsService.save(goodsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品表编辑
     * @param goodsDto 商品表实体类
     * @return
     */
    @ApiOperation("商品表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoods(@RequestBody GoodsDto goodsDto){
        if(goodsDto == null || goodsDto.getId() == null || goodsDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsEntity goodsEntity = ConvertUtil.transformObj(goodsDto,GoodsEntity.class);
        if(goodsService.updateById(goodsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品表删除
     * @param ids 商品表id集合
     * @return
     */
    @ApiOperation("商品表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoods(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品表详情
     * @param id 商品表id
     * @return
     */
    @ApiOperation("商品表详情")
    @GetMapping("/info")
    public RespBody<GoodsDto> infoGoods(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsEntity goodsEntity = goodsService.getById(id);
            GoodsDto goodsDto = ConvertUtil.transformObj(goodsEntity,GoodsDto.class);
        return RespBody.data(goodsDto);
    }


}
